package com.ht.shortlink.project.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ht.shortlink.project.common.convention.result.Result;
import com.ht.shortlink.project.common.convention.result.Results;
import com.ht.shortlink.project.dto.req.ShortLinkBatchCreateReqDTO;
import com.ht.shortlink.project.dto.req.ShortLinkCreateReqDTO;
import com.ht.shortlink.project.dto.req.ShortLinkPageReqDTO;
import com.ht.shortlink.project.dto.req.ShortLinkUpdateReqDTO;
import com.ht.shortlink.project.dto.resp.ShortLinkBatchCreateRespDTO;
import com.ht.shortlink.project.dto.resp.ShortLinkCreateRespDTO;
import com.ht.shortlink.project.dto.resp.ShortLinkGroupCountRespDTO;
import com.ht.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.ht.shortlink.project.handler.CustomBlockHandler;
import com.ht.shortlink.project.service.IShortLinkService;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  短链接服务层
 */
@RestController
@RequiredArgsConstructor
public class ShortLinkController {
    private final IShortLinkService shortLinkService;

    /**
     *  短链接跳转
     */
    @GetMapping("{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response){
        shortLinkService.restoreUrl(shortUri, request, response);
    }

    /**
     *  创建短链接
     */
    @PostMapping("/api/short-link/v1/create")
    @SentinelResource(
            value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    public Result<ShortLinkCreateRespDTO> create(@RequestBody ShortLinkCreateReqDTO requestParam){
        return Results.success(shortLinkService.create(requestParam));
    }

    /**
     *  批量创建短链接
     */
    @PostMapping("/api/short-link/v1/batch-create")
    public Result<ShortLinkBatchCreateRespDTO> batchCreate(@RequestBody ShortLinkBatchCreateReqDTO requestParam){
        return Results.success(shortLinkService.batchCreate(requestParam));
    }

    /**
     *  分页查询短链接
     */
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> queryPage(ShortLinkPageReqDTO requestParam){
        return Results.success(shortLinkService.queryPage(requestParam));
    }

    /**
     *  查询短链接数量
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkGroupCountRespDTO>> listGroupLinkCount(@RequestParam("requestParams") List<String> requestParam){
        return Results.success(shortLinkService.listGroupLinkCount(requestParam));
    }

    /**
     *  更新短链接
     */
    @PostMapping("/api/short-link/v1/update")
    public Result<Void> update(@RequestBody ShortLinkUpdateReqDTO requestParam){
        shortLinkService.update(requestParam);
        return Results.success();
    }
}
